% MUT.m
%
% This function takes the representation of the current population,
% mutates each element with given probability and returns the resulting
% population.
%
% Syntax:	NewChrom = mut(OldChrom,Pm,BaseV)
%
% Input parameters:
%
%		OldChrom - A matrix containing the chromosomes of the
%			   current population. Each row corresponds to
%			   an individuals string representation.
%
%		Pm	 - Mutation probability (scalar). Default value
%			   of Pm = 0.7/Lind, where Lind is the chromosome
%			   length is assumed if omitted.
%
%		BaseV	 - Optional row vector of the same length as the
%			   chromosome structure defining the base of the 
%			   individual elements of the chromosome. Binary
%			   representation is assumed if omitted.
%
% Output parameter:
%
%		NewChrom - A Matrix containing a mutated version of
%			   OldChrom.
%
% Author: Andrew Chipperfield
% Date: 25-Jan-94
%
% Tested under MATLAB v6 by Alex Shenfield (21-Jan-03)

function NewChrom = mymut(OldChrom,Pm1,Pm2)

% get population size (Nind) and chromosome length (Lind)
[Nind, Lind] = size(OldChrom) ;

% perform mutation on chromosome structure
ChromSum = sum(OldChrom,2);
NewChrom1 = OldChrom(ChromSum>4,:);
NewChrom2 = OldChrom(ChromSum<=4,:);
NewChrom1 = NewChrom1-(rand(size(NewChrom1,1),Lind)<Pm1);
NewChrom1(NewChrom1<0)=0;
NewChrom2 = rem(NewChrom2+(rand(size(NewChrom2,1),Lind)<Pm2),2);
NewChrom = [NewChrom1;NewChrom2];
% NewChrom = rem(OldChrom,2);
% find((NewChrom == OldChrom) == 0)